1. Field of the Invention
The present invention relates generally to microprocessor architecture and in particular to a method of identifying and targeting suitable critical sections for intelligently eliding exclusive locks.
2. Description of the Related Art
Throughput computing takes advantage of thread-level parallelism in applications to increase overall performance. In general, the greater amount of thread-level parallelism available in an application, the greater the potential speedup. One impediment to high speedup is the presence of critical sections in the application's code.
A critical section is a section of code that can only be executed by one thread at a time. Some synchronization mechanism is required at the entry and exit of the critical section to ensure exclusive use. Typically, this mechanism involves exclusive locks. Critical sections are an important means to guarantee that while one thread is reading and/or writing a particular piece of data, other threads are not trying to access the same data. By restricting access to one thread at a time, access to the data within the critical section is serialized. Critical sections are often implemented by associating an exclusive lock with each critical section.
Exclusive locks are small data areas set up so that only one thread can change the value from unlocked to locked (and thus acquire the lock) no matter how many other threads are attempting to change the value at the same time. In order to execute a critical section, a thread must acquire the associated exclusive lock before executing any instructions within the critical section. Since only one thread can possess a given lock at any one time, only one thread can be executing inside the corresponding critical section. If a thread t1 wishes to execute a critical section, but the associated lock is held by another thread t2, t1 must wait until the thread holding the lock (t2) exits the critical section and unlocks the lock. Locks are usually implemented using atomic instructions, which perform two or more operations indivisibly. Often, the two operations are a load from memory address, and a conditional or unconditional store to the same address.
Unfortunately, critical sections and their associated locks can reduce performance in multi-threaded applications, sometimes drastically so. The performance reduction can be attributed to two factors. The first and most important factor is lock contention, which occurs when a thread tries to acquire a lock that is already held by another thread, and fails. In most cases, the failing thread waits (i.e., performs no more useful work) until it can acquire the needed lock. In a simple lock implementation, the failing thread continuously tries to acquire the lock until it succeeds. This activity is called spin waiting, and results in wasting valuable processing cycles waiting until the lock variable is unlocked.
Alternatively, the waiting thread's execution can be suspended until the lock becomes free. This has the advantage that the waiting thread doesn't waste processor cycles by endlessly checking if the lock has been unlocked. Unfortunately, the act of suspending and resuming a thread requires a large number of processor cycles itself, sometimes more than spin waiting. In addition, suspending the thread reduces the number of threads in the application that can perform useful work, just as spin waiting does. If the number of available threads is less than the number of available processors, then hardware resources can become idle.
The second most influential cost of locking is executing the underlying atomic instruction that is used to acquire (and sometimes release) the lock. In order to fulfill memory consistency requirements, an atomic instruction may require waiting for pipelines to drain, flushing store buffers, and other performance degrading operations (depending on the processor implementation). Unlike the case with lock contention, the cost of acquiring the lock must be paid whether the lock is contended or not.
Because acquiring and releasing locks can be expensive, and writing code involving large numbers of coordinated critical sections is often difficult (because of problems like deadlock, livelock, etc.) critical sections often protect larger amounts of data than is necessary for application correctness. By encapsulating more data than is required in a critical section, coarse grain locking is implemented. Coarse-grain locking reduces the amount of locking that is required, and makes it easier to write correct multi-threaded applications, but increases lock contention. The increased lock contention is due to two or more threads contending for the same lock even though the threads may access completely disjoint sets of data. Ideally, one would like to have the benefits of coarse-grain locking without increased lock contention.
One technique used to get the benefits of coarse grain locking but without some of the drawbacks is speculative locking, which was introduced as Speculative Lock Elision (SLE) by Rajwar and Goodman in their paper entitled, “Speculative Lock Elision: Enabling Highly Concurrent Multithreaded Execution” presented in the proceedings of the 34th International Symposium on Microarchitecture, Dec. 3 through Dec. 5, 2001, Austin Tex., herein incorporated by reference in its entirety. Speculative locking allows coarse-grain level locking while decreasing lock contention. In speculative locking, a locking thread assumes that the data it accesses in the critical section will not be accessed by another thread until after the locking thread exits the critical section. Thus, two threads can speculatively execute in the same critical section at the same time, provided their data accesses do not interfere, which reduces the lock contention caused by coarse grain locking. It also permits threads to speculatively elide the lock controlling the critical section, since two or more threads may simultaneously execute the same critical section.
An important obstacle in implementing speculative locking is identifying where critical sections begin and end. As atomic instructions are used for purposes other than acquiring critical section locks, assuming that all atomic instructions start critical sections would lead to the speculative locking being used where it should not be. Thus being able to identify the start and end of critical sections is key to getting the benefits of speculative locking.
The SLE work done by Rajwar and Goodman assumes that a sequence of load locked/store conditional (atomic) instructions to the same address signals the start of a critical section. However, this assumption is not actually tested against actual execution sequences. In practice, research on large-scale commercial applications indicates that atomic instructions (such as compare-and-swap) are used for multiple purposes including locking but also for atomically incrementing a value. Atomic instructions are also used, along with regular store instructions, for unlocking as well. Thus a simple heuristic that assumes all atomic instructions begin critical sections is unrealistic for large commercial applications.
Furthermore, hardware mechanisms for speculatively locking are effective only with small critical sections because all loads and stores must be held in hardware buffers while executing in the critical section. Buffering loads and stores is necessary to maintain the effective atomicity of the critical section. Though the majority of dynamic critical sections are small, some critical sections are so large that the number of load/store buffers needed to lock speculation is too expensive to implement. When such critical sections are speculatively executed, the speculation fails because of insufficient buffer size.
Speculative locking can also fail if two threads execute memory operations which interfere with one another while one (or both) of the threads is speculative locking. Unfortunately, the performance impact of failed speculation is significant since the processor must revert to the architectural state at the beginning of the critical section, then re-execute the entire critical section after acquiring the lock. Since the price of misspeculating is so costly, separating those critical sections that are likely to cause speculation to fail from those in which speculation will succeed is necessary. Previous work used very simple static mechanisms to identify and target critical sections for lock elision. However, these simple measures lead to unacceptably high amounts of misspeculation occurring. A hardware mechanism that can dynamically identify those critical sections that are amenable to hardware lock elision is required.